<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" @loaded="loaded" :initConfigData="mainConfigData" :initValue="initValue">
		</om-autoform>
	</om-addpage>
</template>

<script>
	import {
		buildFormData,
		changeEditor,
		configSelectData,
		getFormFields,
		formModelLoad,
		getEditorTitle,
		hasEditor,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				busCode: "",
				customerCacheKey: 'customer-code-cache',
				customerCountryCacheKey: 'customer-country-cache',
				customerUserCacheKey: 'customer-user-cache'
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userId',,'userName','orgId'])
		},
		methods: {
			getPosComboboxByUser(id){
				let _that = this
				_that.om.request(
					"customer/getPosComboboxByUser", {
						userId: id
					}).then(v => {
					_that.$refs.autoform.changeEditor("productInfo", "postId", "data",
						configSelectData(v, "id", "posName"))
						_that.$refs.autoform.initSelectVal("productInfo","postId")
				})
			},
			loaded() {
				let _that = this
				if (!_that.$refs.autoform.formdata.zposId) {
					_that.$refs.autoform.initSelectVal("topinfo", "zposId")
				}
			},
			formSubmit() {
				if (this.edit) {
					this.om.formSave("customer/doUpdate", this.formdata, this)
				} else {
					this.om.formSave("customer/doAdd", this.formdata, this)
				}
			},
			verifyUser(e) {
				console.log(e)
			},
			onSelectPost(e) {
				this.$refs.autoform.formdata.inst = e.instName
				this.$refs.autoform.formdata.instId = e.instId
			},
			cleanPost() {
				this.$refs.autoform.formdata.customerDepartment = ''
			},
			onSelectProvince(e) {
				this.om.request(
					'regions/getRegionsCombobox', {
						parentId: e.id
					}).then(res => {
					this.$refs.autoform.changeEditor("productInfo", "cityId", 'data', configSelectData(res, "id",
						"dicname"))
					this.$refs.autoform.formdata.cityId = ''
					this.$refs.autoform.initSelectIndex("productInfo", "cityId")
				})
			},
			onSelectCity(e) {
				this.om.request(
					'regions/getRegionsCombobox', {
						parentId: e.id
					}).then(res => {
					this.$refs.autoform.changeEditor("productInfo", "districtId", 'data', configSelectData(res,
						"id", "dicname"))
					this.$refs.autoform.formdata.districtId = ''
					this.$refs.autoform.initSelectIndex("productInfo", "districtId")
				})
			},
			onSelectClass(e) {
				this.$refs.autoform.formdata.customerClassCode = e.signcode
			},
			onSelectType(e) {
				// 新增
				if (e.id === 4845) {
					this.$refs.autoform.formdata.customerClassId = ''
					this.$refs.autoform.formdata.customerClassCode = ''
					if(!this.$refs.autoform.formdata.customerOwnerId){
						this.$refs.autoform.formdata.customerOwnerId = this.userId
						this.$refs.autoform.formdata.customerOwner = this.userName
						this.getPosComboboxByUser(this.userId)
					}
					this.$refs.autoform.initSelectIndex('productInfo', 'customerClassId')
					this.$refs.autoform.initSelectIndex('productInfo', 'customerClassCode')
					this.$refs.autoform.changeEditor("productInfo","customerCode","type","textbox")
				} else {
					this.$refs.autoform.formdata.customerCode = ''
					this.$refs.autoform.changeEditor("productInfo","customerCode","type","editlinkbox")
				}
			},
			cleanClass() {
				this.$refs.autoform.formdata.customerClass = ''
				this.$refs.autoform.formdata.customerClassId = ''
				this.$refs.autoform.formdata.customerClassCode = ''
				this.$refs.autoform.formdata.customerCode = ''
				this.$nextTick(() => {
					this.$refs.autoform.initSelectIndex('productInfo', 'customerClassId')
				})
			},
			intLoad(options) {
				let _that = this
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					{
						url: "customer/findAdjustType",
						method: "get"
					},
					urlUtil.sysdictComboboxUrl("39025"),
					urlUtil.sysdictComboboxUrl("38982"), 
					urlUtil.sysdictComboboxUrl("38983"), 
					urlUtil.sysdictComboboxUrl("38984"), 
					urlUtil.sysdictComboboxUrl("38985"),
					urlUtil.sysdictComboboxUrl("38986"),
					urlUtil.sysdictComboboxUrl("38987"),
					urlUtil.sysdictComboboxUrl("38988"),
					urlUtil.sysdictComboboxUrl("38527"),
					urlUtil.sysdictComboboxUrl("39020")
				]
				let fileId = options.fileId
				if (fileId) {
					ajaxArr.push({
						url: "customer/getValue",
						param: {
							fileId: fileId
						}
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("topinfo", "zposId", "data", configSelectData(res[0], "id", "posname"),
						mainData)
					//类别
					changeEditor("productInfo", "typeId", "data", configSelectData(res[1], "id", "dicname"),
						mainData)
					//客户分类
					changeEditor("productInfo", "customerClassId", "data", configSelectData(res[2], "id",
							"dicname"),
						mainData)
					//客户类型
					changeEditor("productInfo", "customerTypeId", "data", configSelectData(res[3], "id",
						"dicname"),
						mainData)
					//客户级别
					changeEditor("productInfo", "customerLevelId", "data", configSelectData(res[4], "id",
							"dicname"),
						mainData)
					//所属行业
					changeEditor("productInfo", "industryId", "data", configSelectData(res[5], "id", "dicname"),
						mainData)
					//产品定位
					changeEditor("productInfo", "productPositionId", "data", configSelectData(res[6], "id",
							"dicname"),
						mainData)
					//产品类型
					changeEditor("productInfo", "productTypeId", "data", configSelectData(res[7], "id", "dicname"),
						mainData)
					//门店销售类型
					changeEditor("productInfo", "storeSalesModeId", "data", configSelectData(res[8], "id",
							"dicname"),
						mainData)
					//门店类型
					changeEditor("productInfo", "storeTypeId", "data", configSelectData(res[9], "id", "dicname"),
						mainData)
					
					//发货方式
					changeEditor("productInfo", "eliveryMethodId", "data", configSelectData(res[10], "id",
							"dicname"),
						mainData)
					//运费承担方式
					changeEditor("productInfo", "freightId", "data", configSelectData(res[11], "id", "dicname"),
						mainData)
					//所属经销商
					// changeEditor("productInfo", "dealerId", "data", configSelectData(res[10], "id", "dicname"),
					// 	mainData)
					changeEditor("productInfo", "customerCode", {
						"linkParam": {
							other: JSON.stringify({}),
							url: "customer/getCustomerCode",
							namefield: "customerId",
							initload: true,
							cachename: _that.customerCacheKey,
							placeholder: "请选择",
						},
					}, null, mainData)
					//客户所属人
					changeEditor("productInfo", "customerOwnerId",'isSearchUser',true, mainData)
					changeEditor("productInfo", "customerOwnerId", {
						link: "../../public/searchUser/searchUser",
						"linkParam": {
							other: JSON.stringify({}),
							isSingle:true,
							cachename: _that.customerUserCacheKey,
						},
					}, null, mainData)
					changeEditor("productInfo", "countryId", {
						link: "../../public/pageSearch/pageSearch",
						"linkParam": {
							other: JSON.stringify({}),
							url: "regions/getCountryList",
							namefield: "dicname",
							searchfield: "q",
							initload: true,
							cachename: _that.customerCountryCacheKey,
							placeholder: "请选择",
						},
					}, null, mainData)
					if (_that.subConfigData["title2"]) {
						let configData = _that.subConfigData["title2"]
						let att = {}
						if (hasEditor('title2', 'userName', configData)) {
							let title  = getEditorTitle('title2', 'userName',configData)
							att.titleTemplate = title+':<%= userName %>'
						}
						if (hasEditor('title2', 'phone', configData)) {
							let title  = getEditorTitle('title2', 'phone',configData)
							att.subtitle1Template = title+'<%= phone %>'
						}
						changeEditor("title2", "dataStr", att, mainData)
					}
					if (_that.subConfigData["title3"]) {
						let configData = _that.subConfigData["title3"]
						let att = {}
						if (hasEditor('title3', 'invoicingAddress', configData)) {
							let title  = getEditorTitle('title3', 'invoicingAddress',configData)
							att.titleTemplate = title+':<%= invoicingAddress %>'
						}
						changeEditor("title3", "invoicStr", att, mainData)
					}
					if (_that.subConfigData["title4"]) {
						let configData = _that.subConfigData["title4"]
						let att = {}
						if (hasEditor('title4', 'bank', configData)) {
							let title  = getEditorTitle('title4', 'bank',configData)
							att.titleTemplate = title+':<%= bank %>'
						}
						changeEditor("title4", "openStr", att, mainData)
					}
					if (_that.subConfigData["title5"]) {
						let configData = _that.subConfigData["title5"]
						let att = {}
						if (hasEditor('title5', 'fileTypeId', configData)) {
							let title  = getEditorTitle('title5', 'fileTypeId',configData)
							att.titleTemplate = title+':<%= fileTypeName %>'
						}
						changeEditor("title5", "uploadStr", att, mainData)
					}
					if (fileId) {
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for (let index in res[12].value) {
							if (fieldArr.includes(index)) {
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata, index, res[12].value[
									index])
								_that.$set(_that.formdata, index, res[12].value[index])
							}
						}
						
						let dataList2 = _that._.cloneDeep(res[12].value.customerDataResponseList)
						_that.subEditorObj["title2"].workList = dataList2
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"dataStr",JSON.stringify(dataList2))
						_that.$set(_that.formdata,'dataStr',JSON.stringify(dataList2))
						delete res[12].value.customerDataResponseList
						let dataList3 = _that._.cloneDeep(res[12].value.customerInvoicList)
						_that.subEditorObj["title3"].workList = dataList3
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"invoicStr",JSON.stringify(dataList3))
						_that.$set(_that.formdata,'invoicStr',JSON.stringify(dataList3))
						delete res[12].value.customerInvoicList
						let dataList4 = _that._.cloneDeep(res[12].value.customerOpenList)
						_that.subEditorObj["title4"].workList = dataList4
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"openStr",JSON.stringify(dataList4))
						_that.$set(_that.formdata,'openStr',JSON.stringify(dataList4))
						delete res[12].value.customerOpenList
						let dataList5 = _that._.cloneDeep(res[12].value.customerUploadList)
						_that.subEditorObj["title5"].workList = dataList5
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata,"uploadStr",JSON.stringify(dataList5))
						_that.$set(_that.formdata,'uploadStr',JSON.stringify(dataList5))
						delete res[12].value.customerUploadList
						
						let ajaxList = [
							{
								url: 'regions/getRegionsCombobox',
								param: {
									parentId: res[12].value.countryId
								}
							}, {
								url: 'regions/getRegionsCombobox',
								param: {
									parentId: res[12].value.provinceId
								}
							}, {
								url: 'regions/getRegionsCombobox',
								param: {
									parentId: res[12].value.cityId
								}
							},  {
								url: 'customer/getPosComboboxByUser',
								param: {
									userId: res[12].value.zuserId
								}
							}
						]
						
						urlUtil.promiseAll(ajaxList, rs => {
							_that.$refs.autoform.changeEditor("productInfo", "provinceId", 'data', configSelectData(rs[0],
								"id", "dicname"))
							_that.$refs.autoform.changeEditor("productInfo", "cityId", 'data', configSelectData(rs[1],
								"id", "dicname"))
							_that.$refs.autoform.changeEditor("productInfo", "districtId", 'data', configSelectData(rs[2],
								"id", "dicname"))
							_that.$refs.autoform.changeEditor("productInfo", "postId", 'data', configSelectData(rs[3],
								"id", "posName"))
							_that.$refs.autoform.initSelectIndex("productInfo", "postId")
							_that.$refs.autoform.initSelectIndex("productInfo", "cityId")
							_that.$refs.autoform.initSelectIndex("productInfo", "provinceId")
							_that.$refs.autoform.initSelectIndex("productInfo", "districtId")
						})

						_that.$nextTick(function(){
							_that.$refs.autoform.initSelectIndex("productInfo", "typeId")
							_that.$refs.autoform.initSelectIndex("productInfo", "customerClassId")
							_that.$refs.autoform.initSelectIndex("productInfo", "customerTypeId")
							_that.$refs.autoform.initSelectIndex("productInfo", "customerLevelId")
							_that.$refs.autoform.initSelectIndex("productInfo", "industryId")
							_that.$refs.autoform.initSelectIndex("productInfo", "productPositionId")
							_that.$refs.autoform.initSelectIndex("productInfo", "productTypeId")
							_that.$refs.autoform.initSelectIndex("productInfo", "storeSalesModeId")
							_that.$refs.autoform.initSelectIndex("productInfo", "storeTypeId")
							_that.$refs.autoform.initSelectIndex("productInfo", "eliveryMethodId")
							_that.$refs.autoform.initSelectIndex("productInfo", "freightId")
							_that.$refs.autoform.initSelectIndex("productInfo", "dealerId")
							_that.$refs.autoform.initSelectIndex('topinfo','zposId')
							if(_that.$refs.autoform.formdata.typeId == 4845){
								_that.$refs.autoform.changeEditor("productInfo","customerCode","type","textbox")
							}else{
								_that.$refs.autoform.changeEditor("productInfo","customerCode","type","editlinkbox")
							}
						})
					}
				}, options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.intLoad(options)
		},
		onUnload() {
			//#ifdef APP-PLUS
			let _that = this;
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					uni.removeStorage({
						key: getWorkCacheKey(fieldId)
					});
				}
			}
			//#endif
		},
		onShow() {
			let _that = this
			if (_that.$refs.autoform) {
				for (var fieldId in _that.subEditorObj) {
					let editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
			}
			uni.getStorage({
				key: _that.customerUserCacheKey,
				success: res => {
					if (res.data) {
						let d = res.data[0]
						_that.$refs.autoform.formdata.customerOwnerId = d.id
						_that.$refs.autoform.formdata.customerOwner = d.username
						_that.getPosComboboxByUser(d.id)
						uni.removeStorage({
							key: _that.customerUserCacheKey
						})
					}
				}
			})
			uni.getStorage({
				key: _that.customerCountryCacheKey,
				success: (res) => {
					if (res.data) {
						_that.$refs.autoform.formdata.countryId = res.data.id;
						_that.$refs.autoform.formdata.country = res.data.dicname;
						_that.om.request(
							'regions/getRegionsCombobox', {
								parentId: res.data.id
							}).then(v => {
							_that.$refs.autoform.changeEditor("productInfo", "provinceId", 'data',
								configSelectData(v, "id", "dicname"))
						})
						uni.removeStorage({
							key: _that.customerCountryCacheKey
						})
					}
				}
			})
			uni.getStorage({
				key: _that.customerCacheKey,
				success: res => {
					if (res.data) {
						let d = res.data
						let formdata = _that.$refs.autoform.formdata
						if(Object.prototype.toString.call(res.data) === "[object Object]"){
							formdata.customerCode = d.customerCode
							formdata.ERPCustomerCode = d.ERPCustomerCode
							formdata.customerName = d.customerName
							formdata.customerShortName = d.customerShortName
							formdata.customerId = d.customerId
						}else{
							formdata.customerCode = d
							formdata.ERPCustomerCode = ''
							formdata.customerName = ''
							formdata.customerShortName = ''
							formdata.customerId = ''
						}
					}
					uni.removeStorage({
						key: _that.customerCacheKey
					})
				}
			})
		}
	}
</script>

<style>
</style>
